package com.weiqiao.report.util;

import com.mingsoft.util.StringUtil;
import com.mongodb.client.model.Filters;
import com.weiqiao.exception.BadRequestException;
import com.weiqiao.utils.StringUtils;
import org.bson.conversions.Bson;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class RequestCheckUtil {
    public static void checkCompanyid(HttpServletRequest request) {
        if (request.getParameter("company_id") == null) {
            throw new BadRequestException("请求必须带有公司id!");
        }
        if (!StringUtil.isInteger(request.getParameter("company_id"))) {
            throw new BadRequestException("公司id必须为数字!");
        }
    }

    public static int checkPageNo(HttpServletRequest request) {
        if (StringUtils.isInteger(request.getParameter("page"))) {
            return Integer.valueOf(request.getParameter("page"));
        }
        return 0;
    }

    public static int checkPageSize(HttpServletRequest request) {
        if (StringUtils.isInteger(request.getParameter("size"))) {
            return Integer.valueOf(request.getParameter("size"));
        }
        return 10;
    }

    public static Bson getInQueryParamter(Bson filter, String paramter_str, String query_field) {
        List<String> list = new ArrayList<>();
        if (paramter_str != null && paramter_str.split(",").length > 0) {
            String[] split = paramter_str.split(",");
            for (int i = 0; i < split.length; i++) {
                list.add(split[i]);
            }
            filter = Filters.and(filter, Filters.in(query_field, list));
        }
        return filter;
    }
}
